package com.crm.dao.impl;

import com.crm.dao.ICustomerDao;
import com.crm.model.Customer;
import com.crm.model.User;

import java.util.List;
import java.util.Date;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

/**
 * @author <a href="mailto:huang0510@hotmail.com">HuangHuang</a>
 * @version $Id$
 */
public class CustomerDao extends BaseDao implements ICustomerDao {

    /**
     * {@inheritDoc}
     */
    public void save(Customer customer) {
        try {
            getHibernateTemplate().saveOrUpdate(customer);
        } catch(Exception e) {
            throw new HibernateException("保存客户信息失败！" + e.getMessage());
        }
    }

    /**
     * {@inheritDoc}
     */
    public List getCustomerAll() {
        return getHibernateTemplate().find("from Customer");
    }

    /**
     * {@inheritDoc}
     */
    public List getCustomerNameAll() {
        return getHibernateTemplate().find("select customerName from Customer");
    }

    /**
     * {@inheritDoc}
     */
    public List getCustomerNameExclude(String id) {
        return getHibernateTemplate().find("select customerName from Customer where id<>?", id);
    }

    public List getRecycleCust(final String uid) {
        return getHibernateTemplate().executeFind(new HibernateCallback() {
            public Object doInHibernate(Session session) {
                return session.getNamedQuery("select_his_customers")
                        .setString("userid", uid)
                        .list();
            }
        });
    }

    public List getCustomerWebsiteExclude(String id) {
        return getHibernateTemplate().find("select website from Customer where id<>?", id);
    }

    /**
     * {@inheritDoc}
     */
    public Customer getCustomerById(String customerId) {
        List list = getHibernateTemplate().find("from Customer where id=?", customerId);
        return getCustomer0(list);
    }

    /**
     * {@inheritDoc}
     */
    public List getCustomersByUserCode(String userCode) {
        return getHibernateTemplate().find("from Customer where user.userCode=?", userCode);
    }

    /**
     * {@inheritDoc}
     */
    public List getCustomerByWeek(String userCode, String weekOfYear) {
        return getHibernateTemplate().find("from Customer where user.userCode=? and weekOfYear=?", new String[]{userCode, weekOfYear});
    }

    /**
     * {@inheritDoc}
     */
    public List getCustomerByWeek(String weekOfYear) {
        return getHibernateTemplate().find("from Customer where weekOfYear=?", weekOfYear);
    }

    /**
     * {@inheritDoc}
     */
    public List getCustomersByKeyword(String userGroup, String userCode, String customerName, String country, String website, String productId, String sourceId, String levelId, String uid, Date fDate, Date tDate) {
        if (User.GROUP_SUPER.equals(userGroup) || User.GROUP_ADMIN.equals(userGroup)) {
            Object[] arguments = new Object[]{"%" + customerName + "%", "%" + country + "%", "%" + website + "%", "%" + productId + "%", "%" + sourceId + "%", "%" + levelId + "%", "%" + uid + "%", fDate, tDate};
            return getHibernateTemplate().find("from Customer where customerName like ? and country like ? and website like ? and product.id like ? and source.id like ? and level.id like ? and user.id like ? and dateOfSave>=? and dateOfSave<=?", arguments);
        } else {
            Object[] arguments = new Object[]{userCode, "%" + customerName + "%", "%" + country + "%", "%" + website + "%", "%" + productId + "%", "%" + sourceId + "%", "%" + levelId + "%", "%" + uid + "%", fDate, tDate};
            return getHibernateTemplate().find("from Customer a where a.user.userCode=? and a.customerName like ? and a.country like ? and a.website like ? and product.id like ? and source.id like ? and level.id like ? and user.id like ? and dateOfSave>=? and dateOfSave<=?", arguments);
        }
    }

    /**
     * {@inheritDoc}
     */
    public void delete(final Customer customer) {
        getHibernateTemplate().execute(new HibernateCallback() {
            public Object doInHibernate(Session session) {
                return session.getNamedQuery("to_his_customers").setString("id", customer.getId()).executeUpdate();
            }
        });        
        getHibernateTemplate().delete(customer);
    }

    /**
     * {@inheritDoc}
     */
    public List getCustomerWebsite() {
        return getHibernateTemplate().find("select website from Customer");
    }

    private Customer getCustomer0(List list) {
        if (list.isEmpty()) {
            return null;
        } else {
            return (Customer) list.get(0);
        }
    }
}
